package cn.iocoder.yudao.module.system.service.usershops;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.usershops.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.usershops.UserShopsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.usershops.UserShopsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户店铺关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserShopsServiceImpl implements UserShopsService {

    @Resource
    private UserShopsMapper userShopsMapper;

    @Override
    public Long createUserShops(UserShopsSaveReqVO createReqVO) {
        // 插入
        UserShopsDO userShops = BeanUtils.toBean(createReqVO, UserShopsDO.class);
        userShopsMapper.insert(userShops);
        // 返回
        return userShops.getId();
    }

    @Override
    public void updateUserShops(UserShopsSaveReqVO updateReqVO) {
        // 校验存在
        validateUserShopsExists(updateReqVO.getId());
        // 更新
        UserShopsDO updateObj = BeanUtils.toBean(updateReqVO, UserShopsDO.class);
        userShopsMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserShops(Long id) {
        // 校验存在
        validateUserShopsExists(id);
        // 删除
        userShopsMapper.deleteById(id);
    }

    private void validateUserShopsExists(Long id) {
        if (userShopsMapper.selectById(id) == null) {
            throw exception(USER_SHOPS_NOT_EXISTS);
        }
    }

    @Override
    public UserShopsDO getUserShops(Long id) {
        return userShopsMapper.selectById(id);
    }

    @Override
    public PageResult<UserShopsDO> getUserShopsPage(UserShopsPageReqVO pageReqVO) {
        return userShopsMapper.selectPage(pageReqVO);
    }

}